package com.zts.base.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zts.util.JsonUtil;
import org.springframework.hateoas.Identifiable;
import se.spagettikod.optimist.Identity;

import java.io.Serializable;

/**
 * Created by Zhangkh on 2017/5/31.
 */
public abstract class BaseEntity implements Identifiable<Long>,Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * 删除标记（0：正常；1：删除；）
     */
    public static final String DEL_FLAG_NORMAL = "0";
    public static final String DEL_FLAG_DELETE = "1";


    /**
     * 实体编号（唯一标识）
     */
    @Identity("id")
    protected Long id;

    public BaseEntity() {
        id=0L;
    }

    public BaseEntity(long id) {
        this();
        this.id = id;
    }

    @Override
    public Long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
    @JsonIgnore
    public Long getEntityId() {return this.id;}

    public void setEntityId(Long id) {this.id = id; }


    /**
     * 插入之前执行方法，子类实现
     */
    public abstract void preInsert();

    /**
     * 更新之前执行方法，子类实现
     */
    public abstract void preUpdate();


    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (null == obj) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        if (!getClass().equals(obj.getClass())) {
            return false;
        }
        BaseEntity that = (BaseEntity) obj;
        return this.getId() == 0L ? false : this.getId() == that.getId();
    }

    @Override
    public String toString() {
        return JsonUtil.toJsonStr(this, true);
    }


}
